<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;

$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['diy']=$this->plugin_setting('diy');
if($_RQ['op']=='main'){
	
	exi($return);
}
/*******op*******/
if($_RQ['op']=='list'){
	//查询员工的订单
	$worker_id=pdo_getcolumn('jy_gohome_worker',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'id');
	$params=array('worker_id'=>$worker_id,'uniacid'=>$_SESSION['uniacid']);
	if(isset($_RQ['status']) && $_RQ['status'] != 10){
		$params['status']=intval($_RQ['status']);
		$return['status']=$_RQ['status'];
	}
	$order_list=pdo_getall('jy_gohome_order',$params,'','','createtime asc');
	if($order_list){
		foreach($order_list as &$item){
			$user=pdo_get('jy_gohome_user',array('id'=>$item['user_id'],'uniacid'=>$_SESSION['uniacid']));
			$serve=pdo_get('jy_gohome_serve',array('id'=>$item['serve_id'],'uniacid'=>$_SESSION['uniacid']));
			$item['user_name']=$user['name'];
			$item['user_mobile']=$user['mobile'];
			$item['serve_title']=$serve['title'];
			$item['serve_image']=$serve['image'];
			$item['package']=json_decode($item['package']);
			$item['createtime']=date('Y-m-d H:i:s', $item['createtime']);
			$serve_times=pdo_getcolumn('jy_gohome_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['serve_id']),'serve_time');
			$serve_times=json_decode($serve_times,true);
			$item['serve_time']=$serve_times[$item['timeid']];
			
		}
	}
	$return['order_list']=$order_list;
	exi($return);
}
//今日订单
if($_RQ['op']=='today'){
	$worker_id=pdo_getcolumn('jy_gohome_worker',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),'id');
	$params=array('worker_id'=>$worker_id,'uniacid'=>$_SESSION['uniacid']);
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['status']=$_RQ['status'];
	}
	$params['day']=date("Y-m-d",time("day"));	
	$order_list=pdo_getall('jy_gohome_order',$params);
	if($order_list){
		foreach($order_list as &$item){
			$user=pdo_get('jy_gohome_user',array('id'=>$item['user_id'],'uniacid'=>$_SESSION['uniacid']));
			$serve=pdo_get('jy_gohome_serve',array('id'=>$item['serve_id'],'uniacid'=>$_SESSION['uniacid']));
			$item['user_name']=$user['name'];
			$item['user_mobile']=$user['mobile'];
			$item['serve_title']=$serve['title'];
			$item['serve_image']=$serve['image'];
			$item['package']=json_decode($item['package']);
			$item['createtime']=date('Y-m-d H:i:s', $item['createtime']);
			$serve_times=pdo_getcolumn('jy_gohome_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['serve_id']),'serve_time');
			$serve_times=json_decode($serve_times,true);
			$item['serve_time']=$serve_times[$item['timeid']];
			
		}
	}
	$return['order_list']=$order_list;
	exi($return);
}
//订单详情
if($_RQ['op']=='detail'){
	if(!$_RQ['id']){
	exi('订单不存在，请重新选择','error');
	}
	$order=pdo_get('jy_gohome_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$order['package']=json_decode($order['package']);
	//用户信息
	$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['user_id']));
	$user=pdo_get('jy_gohome_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['user_id']));
	//项目信息
	$serve=pdo_get('jy_gohome_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['serve_id']));	
	$serve_times=json_decode($serve['serve_time'],true);
	$order['serve_time']=$serve_times[$order['timeid']];
	//print_r($serve);exit;
	//自定义字段数据
	$fileds=pdo_getall('jy_gohome_data',array('uniacid'=>$_SESSION['uniacid'],'order_id'=>$_RQ['id']));
	foreach($fileds as &$item){
		$item['type']=pdo_getcolumn('jy_gohome_fileds',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['fileds_id']),'type');
		$item['title']=pdo_getcolumn('jy_gohome_fileds',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['fileds_id']),'title');
	    if($item['data'] && $item['type']==5){
		$item['data']=rtrim($item['data'], ";");
		$item['data']=ltrim($item['data'], ";");
		}
	}
	$return['order']=$order;
	$return['user']=$user;
	$return['member']=$member;
	$return['serve']=$serve;
	$return['fileds']=$fileds;
	exi($return);
}
//员工确认订单
if($_RQ['op']=='receive'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	//pdo_update('jy_gohome_order',array('status'=>'2'),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$order=pdo_get('jy_gohome_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$user=pdo_get('jy_gohome_user',array('id'=>$order['user_id'],'uniacid'=>$_SESSION['uniacid']));
	$worker=pdo_get('jy_gohome_worker',array('id'=>$order['worker_id'],'uniacid'=>$_SESSION['uniacid']));
	$serve=pdo_get('jy_gohome_serve',array('id'=>$order['serve_id'],'uniacid'=>$_SESSION['uniacid']));
	$serve_times=pdo_getcolumn('jy_gohome_serve',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['serve_id']),'serve_time');
	$serve_times=json_decode($serve_times,true);
	$serve_time=$serve_times[$order['timeid']];
	$order['createtime']=date("Y-m-d h:i:sa", $order['createtime']);
	$postdata = array(
					'first' => array(
						'value' => '您的'.$serve['title'].'业务已成功受理！',
						'color' => '#990000'
					),
					'keyword1' => array(
						'value' => $order['day'].'--'.$serve_time['times']['start'].'-'.$serve_time['times']['end'],
						'color' => '#ff510'
					),
					'keyword2' => array(
						'value' => $serve['title'],
						'color' => '#ff510'
					),
					'keyword3' => array(
						'value' => $order['username'],
						'color' => '#ff510'
					),
					'keyword4' => array(
						'value' => $order['createtime'],
						'color' => '#ff510'
					),
					'remark' => array(
						'value' => '点击查看详细信息',
						'color' => '#ff510'
					),							
				);
	$tpl=$this->plugin_setting('message_tpl');
	$url=custom_url('jy_gohome/order/detail/id='.$_RQ['id'],true);
	//print_r($postdata);exit;
	if($tpl['confirm']){
	cfc('account')->sendTplNotice($order['user_id'],$tpl['confirm'],$postdata,$url,'#980000');
	}
	
	pdo_update('jy_gohome_order',array('status'=>2),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi('确认服务成功');
}
//员工取消订单
if($_RQ['op']=='unreceive'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	pdo_update('jy_gohome_order',array('status'=>'3'),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi('取消服务成功');
}
if($_RQ['op']=='del'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	pdo_delete('jy_gohome_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi('删除预约成功');
}